package yxy.game.pm2.active.module.subordinateclass;

import org.joda.time.DateTime;
import org.redisson.api.RList;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.util.Collection;
import java.util.List;

public class Act_许愿瓶大奖纪录 extends BaseGameRedisBean {

    public int areaid;
    public String name;
    public Currency cur;
    public long time;

    public Act_许愿瓶大奖纪录() {

    }

    public static Act_许愿瓶大奖纪录 create(int areaid, String name, Currency cur) {
        Act_许愿瓶大奖纪录 am = new Act_许愿瓶大奖纪录();
        am.setAreaid(areaid);
        am.setCur(cur);
        am.setName(name);
        am.setTime(DateTime.now().getMillis());
        return am;
    }

    public int getAreaid() {
        return areaid;
    }

    public void setAreaid(int areaid) {
        this.areaid = areaid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Currency getCur() {
        return cur;
    }

    public void setCur(Currency cur) {
        this.cur = cur;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String key() {
        return String.format("AREA:%s:BOTTLEWISH", getAreaid());
    }

    public static String key(int areaid) {
        return String.format("AREA:%s:BOTTLEWISH", areaid);
    }

    public RList<Act_许愿瓶大奖纪录> touch() {
        return redis().client().getList(key());
    }

    /**
     * 历史记录保存数量 -1表示不删除历史记录
     */
    public int historySize() {
        return 20;
    }

    public void add() {
        try {
            if (tryLock()) {
                RList<Act_许愿瓶大奖纪录> list = touch();
                list.add(this);

                if (list.size() > historySize() && historySize() > 0) {
                    list.remove(0);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            unlock();
        }
    }

    public static List<Act_许愿瓶大奖纪录> all(int areaid) {
        return redis().client().getList(key(areaid));
    }

    public Collection<Act_许愿瓶大奖纪录> all() {
        return touch().readAll();
    }

    //从redis移走数据
    public static void removeAllFromRedis(int areaid)  {
        try {
            redis().client().getList(key(areaid)).delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public String lockKey() {
        return String.format("lock:%s", key());
    }

}
